package com.bysj.lms.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

@ApiModel(value = "通用返回模型")
public class  ResultModel<T> implements Serializable {

    public static final String SUCCESS = "success";

    public static final String FAILURE = "failure";

    public static final String ERROR = "error";

    //状态码,前端用于判断状态
    @ApiModelProperty(notes = "状态码", example = "success", dataType = "String", position = 1)
    private String code;

    //额外消息
    @ApiModelProperty(notes = "额外消息,可为空", example = "no more messages", dataType = "String", position = 2)
    private String message;

    //若有数据, 则存储在该属性类
    @ApiModelProperty(notes = "数据对象", position = 3)
    private T data;

    public ResultModel(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }



    public static final <T> ResultModel<T> create(String code, String message, T data) {

        return new ResultModel(code, message, data);
    }

    public static final <T> ResultModel<T> success(String message, T data) {

        return new ResultModel<>(SUCCESS, message, data);
    }

    public static final <T> ResultModel<T> success(String message) {

        return new ResultModel(SUCCESS, message,null);
    }

    public static final ResultModel success() {

        return new ResultModel(SUCCESS, null, null);
    }

    public static final <T> ResultModel<T> failure(String message, T data) {

        return new ResultModel<>(FAILURE, message, data);
    }

    public static final <T> ResultModel<T> failure(String message) {

        return new ResultModel<>(FAILURE, message, null);
    }

    public static final <T> ResultModel<T> failure() {

        return new ResultModel<>(FAILURE, null, null);
    }

    public static final <T> ResultModel<T> error(String message, T data) {

        return new ResultModel<>(ERROR, message, data);
    }

    public static final ResultModel error(String message) {

        return new ResultModel(ERROR, message, null);
    }

    public static final <T> ResultModel<T> error() {

        return new ResultModel<>(ERROR, null, null);
    }



    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
